<template>
    <div class="tinymce-editor">
        <editor v-model="myValue"
                :init="init"
                :disabled="disabled"
                @onClick="onClick">
        </editor>
    </div>
</template>

<script>
    import tinymce from 'tinymce/tinymce'
    import Editor from '@tinymce/tinymce-vue'
    import 'tinymce/themes/silver/theme'
    import 'tinymce/plugins/image'
    import 'tinymce/plugins/media'
    import 'tinymce/plugins/table'
    import 'tinymce/plugins/lists'
    import 'tinymce/plugins/contextmenu'
    import 'tinymce/plugins/wordcount'
    import 'tinymce/plugins/colorpicker'
    import 'tinymce/plugins/textcolor'
    import 'tinymce/plugins/link'
    import 'tinymce/plugins/fullscreen'
    import '@/assets/tinymce/plugins/axupimgs'

    export default {
        components: {
            Editor
        },
        props: {
            //传入一个value，使组件支持v-model绑定
            value: {
                type: String,
                default: ''
            },
            disabled: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                //初始化配置
                init: {
                    height: 650,
                    language: 'zh_CN',
                    default_link_target: "_blank",
                    language_url: '/src/assets/tinymce/langs/zh_CN.js',
                    skin_url: '/src/assets/tinymce/skins/ui/oxide',
                    content_css: "/src/assets/tinymce/skins/content/default/content.css",
                    plugins: 'lists image axupimgs media table textcolor wordcount contextmenu link fullscreen',
                    toolbar: 'undo redo |  styleselect | bold  underline  forecolor backcolor | lists axupimgs media table link | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | fullscreen ',
                    branding: false,
                    menubar: false,//菜单栏
                    statusbar: false,//底部状态栏
                    image_caption: true,//图片标题
                    toolbar_drawer: "false",
                    //重新上传方法
                    images_upload_handler: (blobInfo, success, failure) => {
                        let formData = new FormData();
                        formData.append('file', blobInfo.blob(), blobInfo.blob().name);
                        this.$upload("/dynamic/uploadImage", formData).then(res => {
                            success(res.data);
                        }).catch(() => {
                            failure("上传失败");
                        })
                    }
                },
                myValue: this.value
            }
        },
        mounted() {
            tinymce.init({})
        },
        methods: {
            //添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
            //需要什么事件可以自己增加
            onClick(e) {
                this.$emit('onClick', e, tinymce)
            },
            //可以添加一些自己的自定义事件，如清空内容
            clear() {
                this.myValue = ''
            }
        },
        watch: {
            value(newValue) {
                this.myValue = newValue
            },
            myValue(newValue) {
                this.$emit('input', newValue)
            }
        }
    }

</script>
<style scoped>
</style>